onload=function(){
daohanglan();


//给全选按钮绑定change事件
    let chekAll = document.querySelector("#checkbox");
    chekAll.addEventListener("change",checkALl);
    let chekAls = document.querySelector("#checkbox2");
    chekAls.addEventListener("change",chekAlls);
    //给每个复选按钮添加change事件
    let chks = document.querySelectorAll("input[name='chk']");
    chks.forEach(chk=>{
        chk.addEventListener("change",checkChange);
    })
    //给下拉面板的div绑定点击事件
    let xialas = document.querySelectorAll(".xiala");
    xialas.forEach(xl=>{
        xl.children[0].addEventListener("click",function(){
            let txt = event.target.innerText;//获取当前点击div的文本信息
            //获取前一个兄弟元素
            let prev = xl.previousElementSibling;
            if(txt.indexOf("查看更多信息")>=0){//如果文本信息中包含’收起‘
                event.target.innerText="收起↑";
                prev.style.display="block";
            }else{
                event.target.innerText="查看更多信息↓";
                prev.style.display="none";
            }

        })
    })


    chks.forEach(x=>{
        x.addEventListener("change",yixuan)
    })

    document.querySelector("#checkbox").addEventListener("change",yixuan);
    document.querySelector("#checkbox2").addEventListener("change",yixuan);

    document.querySelector(".cancel").onclick=function (){
        document.querySelector("#zzc").style.top="110px"
        document.querySelector("#zzc").style.display="none";
        document.querySelector(".xiugaibox").style.display="none";
    }

    weilai();

    document.querySelector("#search").addEventListener("keydown",keyboard)
    document.querySelector("#search").addEventListener("input",searchtips)
    document.querySelector(".searchtip").addEventListener("mouseleave",function (){
        document.querySelector(".searchtip").style.display="none"
    })

    document.querySelectorAll(".xlli").forEach(x=>{
        x.addEventListener("mouseover",xllijdbc);
    })
    a =document.querySelector(".showNavtwoson").innerHTML;
    document.querySelector(".newtuijian").addEventListener("mouseover",function (){
        document.querySelector(".showNavtwoson").innerHTML=a;
    })
    document.querySelectorAll(".gnb_nav_center").forEach(x=>{
        x.addEventListener("mouseover",function (){
            document.querySelector(".showNavtwoson").innerHTML=a;
        })
    })

    document.querySelectorAll(".xlli").forEach(x=>{
        x.addEventListener("click",function (){
            let xid = parseInt(event.target.getAttribute("id"));
            location.href="/goodsList?xid="+xid+"&show="+1;
        })
    })

}


function weilai(){
        $(".xiugaictop").on("click",".mbox",gainew)
        $(".xiugaicbtm").on("click",".cbox",gainew)
}

var shifoubeidianji=false;
function gainew(){
    shifoubeidianji=true;
    let memoryValue,colorValue;
    document.querySelectorAll(".mbox").forEach(x=>{
        if (x.style.backgroundColor=="rgb(0, 0, 0)"){
            console.log(x.innerText)
             memoryValue = x.innerText;
        }
    })
    document.querySelectorAll(".cbox").forEach(x=>{
        if (x.style.backgroundColor=="rgb(0, 0, 0)"){
            console.log(x.innerText)
             colorValue = x.innerText;
        }
    })
    $.ajax({
        url:"/goods?v=xiugailast",
        type:"post",
        data:{
            memoryValue,
            colorValue,
        },
    })
}
function xiugaiyes(){
    if (shifoubeidianji){
        $.ajax({
            url:"/goods?v=xiugaiyes",
            type:"post",
            data:{},
            success:((resp)=>{
                console.log(resp)
                if (resp>0){
                    location.href="/goods"
                    alert("修改成功！")
                }else {
                    location.href="/goods"
                    alert("修改失败！")
                }
            })
        })
    }else {
        return
    }

}

function qujiesuan(){
    let arr = new Array();
    let flag=0;
    let chks = document.querySelectorAll("input[name='chk']");
    chks.forEach(x=>{
        if (x.checked){
            flag++;
            arr.push(x.parentNode.childNodes[1].value);
        }
    })
    if (flag>0){
        $.ajax({
            url: "/goods?v=chuan",
            type: "post",
            data:{
                "arr":JSON.stringify(arr)
            },
            success:function (r){
                location.href = "/orderXq?v=shoppingin";
            }
        })


    }else {
        alert("请先勾选您要结算的商品！")
    }

}

function xiugai(){
    shifoubeidianji=false;
    document.querySelector("#zzc").style.top="0px"
    document.querySelector("#zzc").style.display="block";
    document.querySelector(".xiugaibox").style.display="block";
    let sxid = event.target.parentNode.parentNode.childNodes[1].getAttribute("value");
    $.ajax({
        url:"/goods?v=xiugai",
        type:"post",
        data:{
            sxid
        },
        dataType:"json",
        success:((resp)=>{
            console.log(resp)
            let xgtop = document.querySelector(".xiugaictop");
            let xgbtm = document.querySelector(".xiugaicbtm");
            let htmlmemory = ``;
            let htmlcolor = ``;
            resp.queryAllcm[0].forEach(r=>{
                if (r==resp.nowcm[0][1]){
                    htmlmemory+=`<div class="memoryspan mbox now" onclick="dianm()">`+r+`</div>`
                }else {
                    htmlmemory+=`<div class="memoryspan mbox" onclick="dianm()">`+r+`</div>`
                }
                xgtop.innerHTML=`<p>选择内存</p>`+htmlmemory;
            })
            resp.queryAllcm[1].forEach(r=>{
                if (r==resp.nowcm[0][0]){
                    htmlcolor+= `<span class="colorspan cbox now" onclick="dianc()">`+r+`</span>`
                }else {
                    htmlcolor+= `<span class="colorspan cbox" onclick="dianc()">`+r+`</span>`
                }
                xgbtm.innerHTML=`<p>选择颜色</p>`+htmlcolor
                var ss=document.querySelectorAll(".now");
                ss.forEach(s=>{
                    s.style.backgroundColor="rgb(0, 0, 0)"
                })
            })
        })
    })
}

function dianm(){
    document.querySelectorAll(".mbox").forEach(x=>{
        x.style.backgroundColor="rgb(255, 255, 255)"
                x.style.color="#000"
            event.target.style.backgroundColor="rgb(0, 0, 0)";
            event.target.style.color="#fff";
    })
}

function dianc(){
    document.querySelectorAll(".cbox").forEach(x=>{
                x.style.backgroundColor="rgb(255, 255, 255)"
                x.style.color="#000"
            event.target.style.backgroundColor="rgb(0, 0, 0)";
            event.target.style.color="#fff";
    })
}




function piliangdel(){
    var array = new Array();
    let chks = document.querySelectorAll("input[name='chk']");
    chks.forEach(x=>{
        if (x.checked){
            let plId = x.getAttribute("piliang");
            array.push(plId);
        }
    })
    if (array.length>0){
        if(confirm("您确定要删除您选的商品吗？")){
            $.ajax({
                url:"/goods?v=piliangdel",
                type:"post",
                data:{
                    "arr":JSON.stringify(array)
                },
                success:(r=>{
                    if (r>0){
                        alert("删除成功！")
                        location.href="/goods"
                    }else {
                        alert("删除失败！")
                        location.href="/goods"
                    }
                })

            })
        }
    }else {
        alert("请选择您要删除的商品！")
    }

}

function deletecart(gid){
    if (confirm("你确定要删除该商品吗？")){
        $.ajax({
            url:"/goods?v=deletecart",
            type:"post",
            data: {
                gid
            },
            success:(r=>{
                console.log(r)
                if (r>0){
                    alert("删除成功！")
                    location.href="/goods"
                }else {
                    alert("删除失败！")
                    location.href="/goods"
                }
            })
        })
    }

}

function yixuan(){
    let chks = document.querySelectorAll("input[name='chk']");
    let choosejian = 0,choosemoney = 0;
    let nownumber,nowsprice1,nowsprice2;
    chks.forEach(y=>{
        if (y.checked){
            nownumber = parseFloat(y.parentNode.childNodes[11].childNodes[3].innerText);
            nowsprice1 = y.parentNode.childNodes[13].innerText;
            nowsprice2 = parseFloat(nowsprice1.substr(1,nowsprice1.length));
            choosejian+=nownumber;
            choosemoney+=nowsprice2;
        }
    })
    document.querySelector(".nowchoose").innerHTML=choosejian;
    document.querySelector(".nowmoney").innerHTML="￥"+choosemoney+".0";
}


function updateGnum(gid){
    let aftergnum;
    let both = event.target.getAttribute("id");
    let nowgnum = event.target.parentNode.childNodes[3].innerText;
    if (both=="jian"){
        if (nowgnum>1){
            nowgnum--;
            aftergnum=nowgnum;
        }else {
           return;
        }
    }else {
        nowgnum++;
        aftergnum=nowgnum;
    }
    console.log(aftergnum)
    let price = event.currentTarget.parentNode.previousSibling.previousSibling.childNodes[1].innerText
    let prices = price.substr(1,price.length);
    event.currentTarget.parentNode.childNodes[3].innerHTML=aftergnum;
    event.currentTarget.parentNode.nextSibling.nextSibling.innerHTML="￥"+aftergnum*prices+".0"
    let sumjian=0;
    document.querySelectorAll(".gnum").forEach(x=>{
        let sum = parseInt(x.innerHTML);
        sumjian+=sum;
    })
    document.querySelector(".totalgoods").innerHTML=sumjian;
    $.ajax({
        url:"/goods?v=update",
        type:"post",
        data:{
            gid,
            aftergnum
        },
        success:function (r){
            yixuan();
        }
    })

}
function quzhuye(){
    location.href="/index"
}

let chekAlls = function(){
    document.querySelector("#checkbox").checked=document.querySelector("#checkbox2").checked
    //	获取当前全选按钮的选中状态  true/false
    let checked = event.target.checked;
    //	获取页面上所有的其他多选按钮
    let chks = document.querySelectorAll("input[name='chk']");
    //遍历多选按钮
    chks.forEach(chks=>{
        chks.checked = checked;
    })
}
let checkChange=function(){
    //拿到子元素
    let chks = document.querySelectorAll("input[name='chk']");
    let size = 0;
    chks.forEach(chk =>{
        //如果选中了就数量++
        if(chk.checked){
            size++;
        }
    })
    document.querySelector("#checkbox").checked=(size==chks.length);
    document.querySelector("#checkbox2").checked=(size==chks.length);



}
let checkALl = function(){
    document.querySelector("#checkbox2").checked=document.querySelector("#checkbox").checked
    //	获取当前全选按钮的选中状态  true/false
    let checked = event.target.checked;
    //	获取页面上所有的其他多选按钮
    let chks = document.querySelectorAll("input[name='chk']");
    //遍历多选按钮
    chks.forEach(chks=>{
        chks.checked = checked;
    })
}
function isnull(){
    location.href="/login"
}
function iscart(){
    location.href="/goods"
}
function Login(){
    let login = document.querySelector("#Login");
    let levitation = document.querySelector("#levitation");

    login.onmouseenter = function (){
        levitation.style.display = "block";
    }
    levitation.onmouseleave = function (){
        levitation.style.display = "none";
    }
}


function keyboard(){
    let keycode = event.keyCode;
    if (keycode == 13){
        search();
    }
}

function search(){
    let searchvalue = document.querySelector("#search").value;
    if(searchvalue==""){
        event.preventDefault();
    }else {
        location.href="/index?v=search&searchvalue="+searchvalue;
    }

}

function searchtips(){
    if (document.querySelector("#search").value==""){
        document.querySelector(".searchtip").style.display="none"
    }else {
        document.querySelector(".searchtip").style.display="block"
    }
    let searchvalue = document.querySelector("#search").value;
    $.ajax({
        url:"/index?v=searchtip",
        type:"post",
        data:{
            searchvalue
        },
        dataType:"json",
        success:(resp=>{
            if (resp.length>4){
                document.querySelector(".searchtip").style.height="80px";
                document.querySelector(".searchtip").style.overflowY="scroll";
            }else if (resp.length==0){
                document.querySelector(".searchtip").style.height="20px";
                document.querySelector(".searchtip").style.overflowY="";
            }else if(resp.length<4){
                document.querySelector(".searchtip").style.height="auto";
                document.querySelector(".searchtip").style.overflowY="";
            }
            let tipsbox = document.querySelector(".searchtip");
            let html=``;
            resp.forEach(r=>{
                html+=`<div onclick="bindfuture()">`+r.sname+`</div>`
            })
            tipsbox.innerHTML=html;
        })
    })
}

function bindfuture(){
    let divname = event.currentTarget.innerText;
    location.href="/index?v=search&searchvalue="+divname;
}


function xllijdbc(){
    let xid = parseInt(event.currentTarget.getAttribute("id"));
    $.ajax({
        url:"/index?v=searchli",
        type:"post",
        data:{
            xid
        },
        success:((resp)=>{
            let r = JSON.parse(resp);
            let showNavtwoson = document.querySelector(".showNavtwoson");
            let html = ``;
            r.forEach(y=>{
                html+=`<li onclick="chuandetail(${y.sid},${y.lid})">`+y.sname+`</li>`
            })
            showNavtwoson.innerHTML=html;
        })
    })
}

function chuandetail(params1,params2){
    location.href="/productDetails?sid="+params1+"&lid="+params2;
}


function daohanglan() {
    let allLi = document.querySelectorAll(".gnb_nav_center li");
    let show = document.querySelector("#show");
    let zzc = document.querySelector("#zzc");
    let img = document.querySelectorAll(".showNavone li span img");
    let showli = document.querySelectorAll(".showNavone li");
    let showNavtwoson = document.querySelector(".showNavtwoson");
    allLi.forEach(x => {
        x.addEventListener("mouseover", function() {
            document.querySelector("#show").style.height = 680 + "px";
            zzc.style.display = "block";

            showli.forEach(x => {
                x.style.fontWeight = "";
            })

            for (var i = 0; i < img.length; i++) {
                if (i == 0) {
                    img[i].style.display = "block";
                    document.querySelector(".showNavone li").style
                        .fontWeight = "bold";
                } else {
                    img[i].style.display = "none";
                }
            }
            allLi.forEach(y => {
                y.style.background = "#fff";
                y.style.color = "#000";
            })
            event.target.style.background = "#000";
            event.target.style.color = "#ffff";

            showNavtwoson.style.display = "block";
        })

    })
    document.querySelector(".gnb_nav").onmouseleave = function() {
        show.style.height = 0 + "px";
        zzc.style.display = "none";
        allLi.forEach(y => {
            y.style.background = "#fff";
            y.style.color = "#000";
        })
    }

    let cancel = document.querySelector("#cancelshow");
    cancel.addEventListener("click", function() {
        show.style.height = 0 + "px";
    })

    showli.forEach(x => {
        x.addEventListener("mouseover", function() {
            img.forEach(x => {
                x.style.display = "none";
            })
            let hhh = event.currentTarget.querySelector("span img");
            hhh.style.display = "block";
            showli.forEach(y => {
                y.style.fontWeight = "";
            })
            event.currentTarget.style.fontWeight = "bold";

            showNavtwoson.style.display = "none";
            setTimeout(() => {
                showNavtwoson.style.display = "block"
            }, 150);
        })
    })
}